package com.zzl.study.nacos.config;

import com.zzl.study.nacos.NacosServiceMultiManager;
import com.zzl.study.nacos.actuator.NacosMultiDiscoveryEndpoint;
import com.zzl.study.nacos.actuator.NacosMultiDiscoveryHealthIndicator;
import com.zzl.study.nacos.condition.ConditionalOnNacosMultiDiscoveryEnabled;
import com.zzl.study.nacos.properties.NacosMultiDiscoveryProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Nacos多注册中心服务发现actuator端点自动配置类
 *
 * @author: zhangzl
 * @date: 2025/7/16 20:22
 * @version: 1.0
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Endpoint.class)
@ConditionalOnNacosMultiDiscoveryEnabled
public class NacosMultiDiscoveryEndpointAutoConfiguration {

    /**
     * 向Spring容器注入一个Nacos多注册中心服务发现端点
     *
     * @param nacosServiceManager      Nacos多注册中心服务管理器
     * @param nacosDiscoveryProperties Nacos多注册中心属性配置
     * @return Nacos多注册中心服务发现端点
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnAvailableEndpoint
    public NacosMultiDiscoveryEndpoint nacosMultiDiscoveryEndpoint(NacosServiceMultiManager nacosServiceManager, NacosMultiDiscoveryProperties nacosDiscoveryProperties) {
        return new NacosMultiDiscoveryEndpoint(nacosServiceManager, nacosDiscoveryProperties);
    }

    /**
     * 向Spring容器注入一个Nacos多注册中心服务发现健康检查指标
     *
     * @param nacosServiceManager Nacos多注册中心服务管理器
     * @return Nacos多注册中心服务发现健康检查指标
     */
    @Bean
    @ConditionalOnEnabledHealthIndicator("nacos-multi-discovery")
    public HealthIndicator nacosMultiDiscoveryHealthIndicator(NacosServiceMultiManager nacosServiceManager) {
        return new NacosMultiDiscoveryHealthIndicator(nacosServiceManager);
    }

}
